home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Interapplication Communication / TEXTtotypeIntlCoercion / TEXTtotypeIntlCoercion.c
Encoding:
Text File  |  1992-08-26  |  4.0 KB  |  98 lines  |  [TEXT/MPS ]

  1. /*
  2. When the AppleEvent Registry and Inside Mac volume VI were first released,
  3. the documented type for error strings and other stuff was typeChar ('TEXT').
  4. Well, as we went along designing things like AppleScript, we realized
  5. that typeChar was not suitable for a lot of developers and countries.  
  6. Multi-byte scripts couldn't use typeChar and maintain all the information
  7. they needed to, and other Bad Things were implied by typeChar.
  8.  
  9. So, the Winter '92 AE Registry introduced a new type, typeIntlText, defined as
  10. (quoting from the AERegistry)...
  11. A typeIntlText descriptor record contains identifiers for the language and script 
  12. system of text followed by the text itself.
  13. Description    The dataHandle field of a typeIntlText descriptor record contains the 
  14. following:
  15.     a 2-byte script code that identifies the script system of the text
  16.     a 2-byte language code that identifies the language of the text
  17.     the characters in the text (a series of either 1- or 2-byte characters
  18.     ; the script system determines whether the characters are 1 or 2 bytes in length)
  19.  
  20. That's great and wonderful, but heck, a lot of you (and Me) had already 
  21. written code that supplied typeChar for errors and other things that should now be
  22. typeIntlText (note for you programmers: Don't worry, code really is coming soon).
  23.  
  24. What's a Coder to Do?
  25. You can fix your stuff so you supply typeIntlText, but what about all the
  26. other stuff out there that's giving you back typeChar when you want typeIntlText????
  27.  
  28. A Coercion Routine!
  29.  
  30. And here it is.  This coercion routine coerces from typeChar to typeIntlText.
  31. Put this in your code, then when someone sends you typeChar when you've asked for
  32. typeIntlText, this coercion routine will give you what you asked for seamlessly,
  33. you won't even know the coercion happened.
  34.  
  35. EnJoy
  36.  
  37. C.K. Haun
  38. Apple Developer Tech Support
  39. Aug '92.
  40.  
  41. */
  42.  
  43. /* the coercion.  See after this code chunk for code  for installing it */
  44.  
  45. pascal OSErr CoerceTEXTToIntl(DescType origData, Ptr inPtr, Size theSize, DescType toType, long refCon, AEDesc *result)
  46. {
  47. OSErr myErr = noErr;
  48. Handle newDataHandle;
  49. Ptr scratchPtr;
  50. /* first see if the stuff we're coercing is correct.  This _should_ never
  51. really be necessary, since the AEM wouldn't have dispatched to us if it wasn't
  52. right, but Hey, I'm paranoid. */
  53. if(origData == typeText && toType == typeIntlText){
  54.     /* the data handle for typeIntlText contains 
  55.     2 byte script code
  56.     2 byte language code
  57.     the characters.
  58.     So, we'll do something like this....
  59.     */
  60.     /* Get a handle to convert the text to intltext */
  61.     newDataHandle = NewHandle(theSize + 4); /* TEXT size plus those 2 new shorts */
  62.     if(newDataHandle && MemError() == noErr){
  63.         /* got the handle size I needed */
  64.         scratchPtr = *newDataHandle;
  65.         /* I'm going to default the script and language values to system, since I don't know where */
  66.         /* this text is coming from, so I can't assume application script settings */
  67.         /* This means I will be wrong some times, but since this is a stopgap anyway. */
  68.         /* I'll live with it */
  69.         *((short *)scratchPtr = iuCurrentScript;
  70.         scratchPtr = scratchPtr + 2;  /* two bytes further */
  71.         *((short *)scratchPtr = iuCurrentCurLang;
  72.         /* now move the actual bytes */
  73.         HLock(newDataHandle);
  74.         scratchPtr = scratchPtr + 2;  /* two bytes further */
  75.         BlockMove(inPtr,scratchPtr,theSize);
  76.         
  77.         /* put this in the resulting AEDesc for the coercion */
  78.         myErr = AECreateDesc(typeIntlText,*newDataHandle,GetHandleSize(newDataHandle),result);
  79.         /* we've put the data in the desc, get rid of my intermediate handle now */
  80.         DisposHandle(newDataHandle);
  81.         /* and exit, the only error from this bit will be the AECreateDesc error and we can propigate */
  82.         /* that along */
  83.         }
  84.     
  85. } else {
  86. /* bad data passed */
  87.     myErr = errAECoercionFail;
  88. }
  89. return(myErr);
  90. }
  91.  
  92. /* see if there already is one */
  93. installErr = AEGetCoercionHandler(typeChar, typeIntlText, &oldHandler, &oldRefCon, &typeIsDesc, true);
  94. /* if there already was one, I don't install mine */
  95. if(installErr == noErr ){
  96. installErr = AEInstallCoercionHandler(typeChar, typeIntlText, (ProcPtr)CoerceTEXTToIntl, nil, false, true);
  97.  
  98. }